package ac.svn.accesscontrol.controller;

import ac.svn.accesscontrol.ui.window.ProgressGlassPane;
import ac.svn.accesscontrol.ui.window.SplashScreen;

public class SplashScreenController 
{
	private SplashScreen splashScreen;
	private ProgressGlassPane glassPane;
	private Thread splashDisplayThread;
	private int splashScreenTimer;
	private int progressDisplayTimer;


	public SplashScreenController() 
	{
		initComponents();
	}

	private void initComponents() 
	{
		splashScreen = new SplashScreen();
		glassPane = new ProgressGlassPane();
		glassPane.setVisible(true);
		splashScreen.setGlassPane(glassPane);

		splashDisplayThread = new Thread(new Runnable() {

			@Override
			public void run() 
			{
				displaySplashScreen();
			}
		}, "Splash Display Thread");
	}

	protected void displaySplashScreen() 
	{
			glassPane.waitForTime(progressDisplayTimer);
			glassPane.setVisible(true);
			splashScreen.setVisible(true);
			try
			{
				Thread.sleep(splashScreenTimer);
			}
			catch (InterruptedException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				splashScreen.setVisible(false);
			}
	}

	public void showSplashScreen(int splashScreenTimer,int progressTimer)
	{
		this.splashScreenTimer = splashScreenTimer;
		this.progressDisplayTimer = progressTimer;

		try
		{
			if(false == splashDisplayThread.isAlive())
			{
				splashDisplayThread.start();
			}
		}
		catch(Exception e)
		{

		}
	}
}
